package com.kotlin.flong.reflection

import kotlin.reflect.full.createInstance
import kotlin.reflect.full.functions

/**
 * @Author:liangjl
 * @Date: 2020/7/12-8:52
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
class MyTest12(var name: String) {
    var price = 0.0

    constructor() : this("未知商品") {
        this.price = 0.0
    }

    constructor(name: String, price: Double) : this(name) {
        this.price = price
    }
}

fun main() {

    var myTest12 = MyTest12::class
    var myObj = myTest12.createInstance()

    println(myObj.price)
    println(myObj.name)

    var cons = myTest12.constructors
    cons.forEach {
        //constructor构造方法通过参数去判断构造方法的参数数量为2，就调用call传入2个参数
        if (it.parameters.size == 2) {

            var instance = it.call("kotlin", 80.2)
            println(instance.price)
            println(instance.name)
        }
    }
}